<?php
session_start();
include('inc.db.php');

$_SESSION['aantalA'] ='';
$_SESSION['aantalB'] ='';
$_SESSION['aantalC'] = '';
$_SESSION['aantalD'] = '';
$_SESSION['aantalE'] = '';


echo '<form action="aanpassen.php" method="post">
	  <label>Voer bestelnummer in</label><input type="text" name="id" value='.$_SESSION['id2'].'>
	  <input type="submit" name="haalop" value="haal bestelling op"/>
	  </form>';

if(isset($_POST['haalop'])){
	$_SESSION['id2'] = $_POST['id'];
	$query = mysql_query("SELECT * FROM bestellingen WHERE id='".$_SESSION['id2']."'") or die (mysql_error());
	
	while($row = mysql_fetch_array($query))
		{
			$bestelling[] = $row;	
		}				
	echo '<h1> Uw bestelling </h1>';
	foreach($bestelling as $value => $key){
		echo 'email: '.$key[6].'</br></br>
			  Aantal product A: '.$key[1].'</br>
			  Aantal product B: '.$key[2].'</br>
			  Aantal product C: '.$key[3].'</br>
			  Aantal product D: '.$key[4].'</br>
			  Aantal product E: '.$key[5].'</br></br>
			  Totale prijs: '.$key[7].' Euro</br>';
	}	
}

echo '<h1> Verander de bestelling </h1>';

echo "<form action='aanpassen.php' method='post'>
<b>Bestelformulier</b>
<p>
Selecteer de gewenste producten en aantallen in.
</p>
<p>
     product A <label> (2 euro) </label><input type='checkbox' name='productA' /><label> aantal </label><input type='text' value='0' name='aantalA' />
<br/>product B <label> (5 euro) </label><input type='checkbox' name='productB' /><label> aantal </label><input type='text' value='0' name='aantalB' />
<br/>product C <label> (6 euro) </label><input type='checkbox' name='productC' /><label> aantal </label><input type='text' value='0' name='aantalC' />
<br/>product D <label> (7 euro) </label><input type='checkbox' name='productD' /><label> aantal </label><input type='text' value='0' name='aantalD' />
<br/>product E <label> (4 euro) </label><input type='checkbox' name='productE' /><label> aantal </label><input type='text' value='0' name='aantalE' />
<br/><input type='submit' name='aanpassen' value='Pas bestelling aan' />
</form>";

if(isset($_POST['aanpassen'])){
	if(isset($_POST['productA'])){
		$_SESSION['aantalA'] = $_POST['aantalA']; 
	}else{
		$_SESSION['aantalA'] = '0';
	}
	
	if(isset($_POST['productB'])){ 
		$_SESSION['aantalB'] = $_POST['aantalB']; 
	}else{
		$_SESSION['aantalB'] = '0';
	}
	
	if(isset($_POST['productC'])){ 
		$_SESSION['aantalC'] = $_POST['aantalC']; 
	}else{
		$_SESSION['aantalC'] = '0';
	}
	
	if(isset($_POST['productD'])){ 
		$_SESSION['aantalD'] = $_POST['aantalD']; 
	}else{
		$_SESSION['aantalD'] = '0';
	}
	
	if(isset($_POST['productE'])){ 
		$_SESSION['aantalE'] = $_POST['aantalE']; 
	}else{
		$_SESSION['aantalE'] = '0';
	}
	
	$kostena = $_SESSION['aantalA'] * 2;
	$kostenb = $_SESSION['aantalB'] * 5;
	$kostenc = $_SESSION['aantalC'] * 6;
	$kostend = $_SESSION['aantalD'] * 7;
	$kostene = $_SESSION['aantalE'] * 4;

	$totaal = $kostena + $kostenb + $kostenc + $kostend + $kostene;

   $datum = date('Y:m:d');
	if(!mysql_query("UPDATE bestellingen SET 
				producta='".mysql_real_escape_string($_SESSION['aantalA'])."',
				productb='".mysql_real_escape_string($_SESSION['aantalB'])."',
				productc='".mysql_real_escape_string($_SESSION['aantalC'])."', 
				productd='".mysql_real_escape_string($_SESSION['aantalD'])."',
				producte='".mysql_real_escape_string($_SESSION['aantalE'])."',
				totaalprijs='".mysql_real_escape_string($totaal)."',
				datum='".mysql_real_escape_string($datum)."' WHERE id=".$_SESSION['id2'].""))
	
	echo '<h1> Bestelling niet aangepast </h1>';
	else
		echo '<h1> Bestelling aangepast </h1>
		<p> Bekijk nogmaals uw bestelling </p>';
}
?>